package DataStrucures;
//数据结构队列
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue=new ArrayQueue(5);
        for (int i = 0; i < 5; i++) {
            arrayQueue.addQueue(i);
        }
        System.out.println(arrayQueue.getQueue());

        arrayQueue.showQueue();
    }
}
class ArrayQueue{
    private int maxSize;//最大容量
    private int front;//队头
    private int rear;//队尾
    private int[] arr;
    public ArrayQueue(int arrMaxSize){//创建一个队列
        maxSize=arrMaxSize;
        arr=new int[maxSize];
        front=-1;
        rear=-1;
    }
    public boolean isFull(){//是否已满
        return rear==maxSize-1;
    }
    //是否为空
    public boolean isEmpty(){
        return rear==front;
    }
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列为满");
            return;
        }
        rear++;
        arr[rear]=n;
    }
    public  int getQueue(){
        if(isEmpty()){

            throw new RuntimeException("队列为空");
            //这里不能直接返回-1，队头元素有可能就是-1可能会冲突

        }
        front++;
        return arr[front];
    }
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空，没有元素");
            return;
        }
        for (int i = front+1; i <= rear; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}